﻿Imports System.Windows.Forms

Public Class frmSelectOption

    Public Enum enumResult
        Button1
        Button2
        Button3
        Button4
        Button5
        Cancel
    End Enum

    Public Result As enumResult = enumResult.Cancel

    Private Sub btnAppend_Click(sender As Object, e As System.EventArgs) Handles btn2.Click, btnCancel.Click, btn1.Click, btn5.Click, btn4.Click, btn3.Click
        If sender Is btn1 Then
            Result = enumResult.Button1
        ElseIf sender Is btn2 Then
            Result = enumResult.Button2
        ElseIf sender Is btn3 Then
            Result = enumResult.Button3
        ElseIf sender Is btn4 Then
            Result = enumResult.Button4
        ElseIf sender Is btnCancel Then
            Result = enumResult.Cancel
        End If

        Close()
    End Sub

    Public Shared DoThisFromNowOn As New Generic.Dictionary(Of String, enumResult)

    ''' <summary>
    ''' Configure the message form with a variable number of buttons, display it, and return which button was clicked
    ''' </summary>
    ''' <param name="ParentForm">Form that this should be displayed on top of</param>
    ''' <param name="FormCaption">Text in caption bar of form (this should be UNIQUE in the application)</param>
    ''' <param name="HeaderText">Longer text describing the issue and asking what option is desired</param>
    ''' <param name="IgnoreText">If you want an Ignore checkbox visible, include text here; if is checked when a large button is clicked, for remainder of session the form won't be displayed but the selected result will be returned</param>
    ''' <param name="ButtonText">Up to five captions for buttons</param>
    ''' <returns>Which button was clicked</returns>
    ''' <remarks></remarks>
    Public Shared Function SelectOption(ParentForm As Form, FormCaption As String, HeaderText As String, IgnoreText As String, ParamArray ButtonText() As String) As enumResult

        Dim PriorResult As enumResult = enumResult.Cancel

        If DoThisFromNowOn.TryGetValue(FormCaption, PriorResult) AndAlso PriorResult <> enumResult.Cancel Then
            Return PriorResult
        End If

        If ButtonText.Length > 5 Then Throw New IndexOutOfRangeException

        With New frmSelectOption
            .Text = FormCaption
            .txtHeader.Text = HeaderText

            For i As Integer = 1 To 5
                Dim btn As Button = Choose(i, .btn1, .btn2, .btn3, .btn4, .btn5)
                If i <= ButtonText.Length AndAlso Not String.IsNullOrEmpty(ButtonText(i - 1)) Then
                    btn.Text = ButtonText(i - 1)
                Else
                    btn.Visible = False
                End If
            Next

            If String.IsNullOrEmpty(IgnoreText) Then
                .chkIgnore.Visible = False
            Else
                .chkIgnore.Text = IgnoreText
            End If

            .ShowDialog(ParentForm)

            If .chkIgnore.Checked Then
                If DoThisFromNowOn.ContainsKey(FormCaption) Then DoThisFromNowOn.Remove(FormCaption)
                DoThisFromNowOn.Add(FormCaption, .Result)
            End If

            Return .Result
        End With
    End Function
End Class
